const local ={
	
	
	
	
	setStorage(key,value,duration){
		var data ={
			value,
			expiryTime:!duration || isNaN(duration) ? 0:this.getCurrentTimeStamp() +parseInt(duration)
		}
		localStorage[key] = JSON.stringify(data);
	},
	getStorage(key){
		var data = localStorage[key];
        if(!data || data === "null"){
            return null;
        }
        var now = this.getCurrentTimeStamp();
        var obj;
        try{
            obj = JSON.parse(data);
        } catch(e){
            return null;
        }
        if(obj.expiryTime ===0 || obj.expiryTime >now){
            return obj.value;
        }
        return null;
	},
	getCurrentTimeStamp(){
        return Date.parse(new Date());
    },
    setSearchHistory(shistory){
    	this.setStorage("searchHistory",shistory);
    },
    getSearchHistory(){
    	return this.getStorage("searchHistory");
    },
    removeSearchHistory(){
    	 this.removeStorage('searchHistory');
    },
    setUserInfo(user){
    	this.setStorage("userInfo",user);
    },
    getUserInfo(user){
    	return this.getStorage("userInfo");
    },
    removeStorage(key){
        // console.log(key);
        // console.log(localStorage[key])
        window.localStorage.removeItem(key);
    },
    quitUser(){
        this.removeStorage('userInfo');
    }
}

export default local;
